const A = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoperId: '',
    pageNum: 1,
    pageSize: 10,
    confirmFlag: false,
    orderList: [],
    noPicking: false,
    noOrder: false,
    showOrder: false,
    noMore: false,
    animationData: true,
    content: "提货成功",
    success: true,
    fail: true,
  },

  // 获取用户提货单列表
  getUserOrderList() {
    // if (this.data.noMore) {
    //   return;
    // }
    let merchantId = this.data.shoperId;
    let userId = wx.getStorageSync('userId');
    let config = {
      merchantId,
      userId,
      pageNum: this.data.pageNum++,
      pageSize: this.data.pageSize
    };
    A.updata.getUserOrderList(config).then(res => {
      console.log("提货单");
      console.log(res);
      this.setData({
        orderList: this.data.orderList.concat(res.list)
      })
      this.setData({
        noMore: this.data.orderList.length >= res.total
      })
    })

  },

  // 提货
  chargeOrder(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.id);
    this.setData({
      confirmFlag: true,
      orderId: e.currentTarget.dataset.id
    })
  },

  // 取消提货
  cancelCofirm() {
    this.setData({
      confirmFlag: false,
      orderId: ''
    })
  },

  // 确认提货
  okConfirm() {
    this.setData({
      confirmFlag: false
    })
    const config = {
      orderId: this.data.orderId,
      merchantId: this.data.shoperId
    }
    A.updata.updateChargeOff(config).then(res => {
      console.log(res);
      if(res.code == 200) {
        wx.showToast({
          title: '提货成功',
          icon: "none"
        })
        setTimeout(()=> {
          this.setData({
            pageNum: 1,
            orderList: []
          });
          this.getUserOrderList();
        }, 1500)
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },


  showToast(val, state) {

    this.setData({
      animationData: false,
      content: val,
    })
    if (state == 1) {
      this.setData({
        success: false,
        fail: true,
      })
    }

    /**
     * 延时消失
     */
    setTimeout(function () {
      this.setData({
        animationData: true
      })
    }.bind(this), 1500)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let shoperId = options.shoperId;
    this.setData({ shoperId });
    this.getUserOrderList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getUserOrderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})